<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="未审核" :value="1" />
          <el-option label="已通过" :value="2" />
          <el-option label="未通过" :value="3" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索结果
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="reportList"
      @selection-change="handleSelectionChange"
      stripe
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="left" prop="companyName" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditCode" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="warning">未审核</el-tag>
          <el-tag v-if="scope.row.status === 2" type="success">已通过</el-tag>
          <el-tag v-if="scope.row.status === 3" type="danger">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row)"
          >
            审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-card>
          <div slot="header" class="header">企业基本信息</div>
          <el-form-item label="企业名称" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入企业名称" />
          </el-form-item>
          <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
            <el-input v-model="form.unifiedSocialCreditCode" placeholder="请输入统一社会信用代码" />
          </el-form-item>
          <el-form-item label="企业成立时间" prop="establishmentDate">
            <el-date-picker
              v-model="form.establishmentDate"
              type="date"
              placeholder="选择企业成立时间"
              format="yyyy-MM-dd"

            />
          </el-form-item>
          <el-form-item label="企业入驻时间" prop="settlementDate">
            <el-date-picker
              v-model="form.settlementDate"
              type="date"
              placeholder="选择企业入驻时间"
              format="yyyy-MM-dd"

            />
          </el-form-item>
          <el-form-item label="企业登记注册类型" prop="registrationType">
            <el-select v-model="form.registrationType" placeholder="请选择企业登记注册类型">
              <el-option
                v-for="item in registrationTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="企业所属技术领域" prop="techField">
            <el-select v-model="form.techField" placeholder="请选择技术领域">
              <el-option
                v-for="item in techFieldOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="行业类别" prop="industryCategory">
            <el-input v-model="form.industryCategory" placeholder="请输入行业类别代码" />
          </el-form-item>
          <el-form-item label="是否与创业导师建立辅导关系" prop="mentorshipRelation">
            <el-radio-group v-model="form.mentorshipRelation">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="企业纳税人类型" prop="taxpayerType">
            <el-radio-group v-model="form.taxpayerType">
              <el-radio :label="1">一般纳税人</el-radio>
              <el-radio :label="0">小规模纳税人</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="主要负责人性别" prop="principalGender">
            <el-radio-group v-model="form.principalGender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否毕业企业" prop="isGraduated">
            <el-radio-group v-model="form.isGraduated">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否被兼并或收购" prop="isMergedAcquired">
            <el-radio-group v-model="form.isMergedAcquired">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否在国内外资本市场挂牌、上市" prop="isListed">
            <el-radio-group v-model="form.isListed">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="企业主要负责人创业特征" prop="entrepreneurType">
            <el-select v-model="form.entrepreneurType" placeholder="请选择创业特征">
              <el-option label="大学生创业" :value="1" />
              <el-option label="留学归国人员" :value="2" />
              <el-option label="其他" :value="5" />
            </el-select>
          </el-form-item>
          <el-form-item label="是否高新技术企业" prop="isHighTech">
            <el-radio-group v-model="form.isHighTech">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            v-if="form.isHighTech === 1"
            label="高新技术企业证书编号"
            prop="highTechCertNo"
          >
            <el-input v-model="form.highTechCertNo" placeholder="请输入高新技术企业证书编号" />
          </el-form-item>
          <el-form-item label="是否科技型中小企业" prop="isTechSme">
            <el-radio-group v-model="form.isTechSme">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="在孵企业总收入（千元）" prop="totalIncome">
            <el-input-number v-model="form.totalIncome" :min="0" placeholder="请输入在孵企业总收入" />
          </el-form-item>
          <el-form-item label="在孵企业净利润（千元）" prop="netProfit">
            <el-input-number v-model="form.netProfit" :min="0" placeholder="请输入在孵企业净利润" />
          </el-form-item>
          <el-form-item label="在孵企业出口总额（千元）" prop="exportVolume">
            <el-input-number v-model="form.exportVolume" :min="0" placeholder="请输入在孵企业出口总额" />
          </el-form-item>
          <el-form-item label="在孵企业研究与试验发展（R&D）经费支出（千元）" prop="rdExpenditure">
            <el-input-number v-model="form.rdExpenditure" :min="0" placeholder="请输入研究与试验发展经费支出" />
          </el-form-item>
          <el-form-item label="在孵企业实际上缴税费（千元）" prop="taxPayment">
            <el-input-number v-model="form.taxPayment" :min="0" placeholder="请输入在孵企业上缴税额" />
          </el-form-item>
          <el-form-item label="在孵企业从业人员（人）" prop="employees">
            <el-input-number v-model="form.employees" :min="0" placeholder="请输入在孵企业从业人员数" />
          </el-form-item>
          <el-form-item label="其中：博士（人）" prop="doctorNum">
            <el-input-number v-model="form.doctorNum" :min="0" placeholder="请输入博士人数" />
          </el-form-item>
          <el-form-item label="大专以上（人）" prop="collegeAbove">
            <el-input-number v-model="form.collegeAbove" :min="0" placeholder="请输入大专以上人数" />
          </el-form-item>
          <el-form-item label="吸纳应届大学毕业生（人）" prop="freshGraduate">
            <el-input-number v-model="form.freshGraduate" :min="0" placeholder="请输入吸纳应届大学毕业生人数" />
          </el-form-item>
          <el-form-item label="留学人员（人）" prop="returnee">
            <el-input-number v-model="form.returnee" :min="0" placeholder="请输入留学人员数" />
          </el-form-item>
        </el-card>

        <el-card>
          <div slot="header" class="header">知识产权情况</div>
          <el-form-item label="当年知识产权申请数（件）" prop="ipApplication">
            <el-input-number v-model="form.ipApplication" :min="0" placeholder="请输入知识产权申请数" />
          </el-form-item>
          <el-form-item label="当年知识产权授权数（件）" prop="ipGranted">
            <el-input-number v-model="form.ipGranted" :min="0" placeholder="请输入知识产权授权数" />
          </el-form-item>
          <el-form-item label="当年知识产权授权数-发明专利（件）" prop="inventionPatentGranted">
            <el-input-number v-model="form.inventionPatentGranted" :min="0" placeholder="请输入发明专利授权数" />
          </el-form-item>
          <el-form-item label="拥有有效知识产权数（件）" prop="validIp">
            <el-input-number v-model="form.validIp" :min="0" placeholder="请输入有效知识产权数" />
          </el-form-item>
          <el-form-item label="拥有有效知识产权数-发明专利（件）" prop="validInventionPatent">
            <el-input-number v-model="form.validInventionPatent" :min="0" placeholder="请输入有效发明专利数" />
          </el-form-item>
          <el-form-item label="软件著作权（件）" prop="softwareCopyright">
            <el-input-number v-model="form.softwareCopyright" :min="0" placeholder="请输入软件著作权数" />
          </el-form-item>
          <el-form-item label="植物新品种（个）" prop="plantVariety">
            <el-input-number v-model="form.plantVariety" :min="0" placeholder="请输入植物新品种数" />
          </el-form-item>
          <el-form-item label="集成电路布图（个）" prop="icLayout">
            <el-input-number v-model="form.icLayout" :min="0" placeholder="请输入集成电路布图数" />
          </el-form-item>
          <el-form-item label="购买国外专利（件）" prop="foreignPatentPurchase">
            <el-input-number v-model="form.foreignPatentPurchase" :min="0" placeholder="请输入购买国外专利数" />
          </el-form-item>
        </el-card>

        <el-card>
          <div slot="header" class="header">技术合同情况</div>
          <el-form-item label="技术合同成交数量（项）" prop="techContractNum">
            <el-input-number v-model="form.techContractNum" :min="0" placeholder="请输入技术合同成交数量" />
          </el-form-item>
          <el-form-item label="技术合同成交额（千元）" prop="techContractAmount">
            <el-input-number v-model="form.techContractAmount" :min="0" placeholder="请输入技术合同成交额" />
          </el-form-item>
        </el-card>

        <el-card>
          <div slot="header" class="header">承担科技计划项目情况</div>
          <el-form-item label="当年承担国家级科技计划项目数（项）" prop="nationalTechProject">
            <el-input-number v-model="form.nationalTechProject" :min="0" placeholder="请输入国家级科技计划项目数" />
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="'审核 - ' + auditForm.companyName" :visible.sync="auditOpen" width="50%" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="120px">
        <el-form-item label="审核状态">
          <el-radio-group v-model="auditForm.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报出日期" prop="reportDate">
          <el-date-picker
            v-model="form.reportDate"
            type="date"
            placeholder="选择报出日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="单位详细名称" prop="companyName" :required="true">
          <el-input v-model="auditForm.companyName" placeholder="请输入单位详细名称" />
        </el-form-item>
        <el-form-item label="单位负责人" prop="companyPrincipal" :required="true">
          <el-input v-model="auditForm.companyPrincipal" placeholder="请输入单位负责人" />
        </el-form-item>
        <el-form-item label="统计负责人" prop="statisticsPrincipal" :required="true">
          <el-input v-model="auditForm.statisticsPrincipal" placeholder="请输入统计负责人" />
        </el-form-item>
        <el-form-item label="填表人" prop="filler" :required="true">
          <el-input v-model="auditForm.filler" placeholder="请输入填表人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone" :required="true">
          <el-input v-model="auditForm.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  GetUserDatazongchuang,
  AddDatazongchuang,
  UpdataUserDatazongchuang,
  Datazongchuangdetial,
  Deatalzongchuangdetial, GetUserDatazongchuangStatusGT0
} from '@/api/system/DataFrom';

export default {
  name: "ZongChuangReport",

  data() {
    // 定义企业登记注册类型映射
    // 定义企业登记注册类型映射
    const registrationTypeMap = {
      '110': '国有',
      '120': '集体',
      '130': '股份合作',
      '141': '国有联营',
      '142': '集体联营',
      '143': '国有与集体联营',
      '149': '其他联营',
      '151': '国有独资公司',
      '159': '其他有限责任公司',
      '160': '股份有限公司',
      '171': '私营独资',
      '172': '私营合伙',
      '173': '私营有限责任公司',
      '174': '私营股份有限公司',
      '190': '其他',
      '210': '与港澳台商合资经营',
      '220': '与港澳台商合作经营',
      '230': '港澳台商独资',
      '240': '港澳台商投资股份有限公司',
      '290': '其他港澳台投资',
      '310': '中外合资经营',
      '320': '中外合作经营',
      '330': '外资企业',
      '340': '外商投资股份有限公司',
      '390': '其他外商投资'
    };

    // 生成企业登记注册类型选项
    const registrationTypes = Object.entries(registrationTypeMap).map(([value, label]) => ({
      value,
      label
    }));

    // 定义企业所属技术领域映射
    const techFieldMap = {
      '1': '电子信息',
      '2': '先进制造',
      '3': '航空航天',
      '4': '现代交通',
      '5': '生物医药与医疗器械',
      '6': '新材料',
      '7': '新能源与节能',
      '8': '环境保护',
      '9': '地球、空间与海洋',
      '10': '核应用技术',
      '11': '现代农业',
      '12': '文化创意'
    };

    // 生成企业所属技术领域选项
    const techFieldOptions = Object.entries(techFieldMap).map(([value, label]) => ({
      value,
      label
    }));
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报表列表
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示审核弹窗
      auditOpen: false,
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
        companyName: null,
        status: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {
        id: null,
        status: null,
        reportDate: null,
        companyName: null,
        companyPrincipal: null,
        statisticsPrincipal: null,
        filler: null,
        contactPhone: null
      },
      // 下拉选项
      registrationTypes, // 确保在 data 中返回 registrationTypes
      techFieldOptions,  // 确保在 data 中返回 techFieldOptions
      // 表单验证规则
      rules: {
        companyName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: "统一社会信用代码不能为空", trigger: "blur" }
        ]
      },
      // 审核表单验证规则
      auditRules: {
        companyName: [
          { required: true, message: "单位详细名称不能为空", trigger: "blur" }
        ],
        companyPrincipal: [
          { required: true, message: "单位负责人不能为空", trigger: "blur" }
        ],
        statisticsPrincipal: [
          { required: true, message: "统计负责人不能为空", trigger: "blur" }
        ],
        filler: [
          { required: true, message: "填表人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报表列表 */
    getList() {
      this.loading = true;
      GetUserDatazongchuangStatusGT0(this.queryParams).then(response => {
        // 过滤掉状态为0的数据
        this.reportList = response.data.records.filter(item => item.status !== 0);
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 审核取消按钮
    cancelAudit() {
      this.auditOpen = false;
      this.resetAudit();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        unifiedSocialCreditCode: null,
        establishmentDate: null,
        settlementDate: null,
        registrationType: null,
        techField: null,
        keyTechField: null,
        industryCategory: null,
        mentorshipRelation: 0,
        taxpayerType: 0,
        principalGender: 0,
        isGraduated: 0,
        isMergedAcquired: 0,
        isListed: 0,
        entrepreneurType: null,
        isHighTech: 0,
        highTechCertNo: null,
        isTechSme: 0,
        totalIncome: 0,
        netProfit: 0,
        exportVolume: 0,
        rdExpenditure: 0,
        taxPayment: 0,
        employees: 0,
        doctorNum: 0,
        collegeAbove: 0,
        freshGraduate: 0,
        returnee: 0,
        ipApplication: 0,
        ipGranted: 0,
        inventionPatentGranted: 0,
        validIp: 0,
        validInventionPatent: 0,
        softwareCopyright: 0,
        plantVariety: 0,
        icLayout: 0,
        foreignPatentPurchase: 0,
        techContractNum: 0,
        techContractAmount: 0,
        nationalTechProject: 0,
        reportDate: null,
        status: 1 // 新增时默认状态为未审核（1）
      };
      this.resetForm("form");
    },
    // 审核表单重置
    resetAudit() {
      this.auditForm = {
        id: null,
        status: null,
        reportDate: null,
        companyName: null,
        companyPrincipal: null,
        statisticsPrincipal: null,
        filler: null,
        contactPhone: null
      };
      this.resetForm("auditForm");
    },


 /** 搜索结果按钮操作 */
handleQuery() {
  this.queryParams.current = 1;
  this.getList();
},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      Datazongchuangdetial(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报表";
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.resetAudit();
      this.auditForm.id = row.id;
      this.auditForm.companyName = row.companyName;
      this.auditOpen = true;
    },
    /** 提交审核 */
    submitAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          const data = {
            id: this.auditForm.id,
            status: this.auditForm.status,
            reportDate: this.auditForm.reportDate,
            companyName: this.auditForm.companyName,
            companyPrincipal: this.auditForm.companyPrincipal,
            statisticsPrincipal: this.auditForm.statisticsPrincipal,
            filler: this.auditForm.filler,
            contactPhone: this.auditForm.contactPhone
          };
          UpdataUserDatazongchuang(data).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            UpdataUserDatazongchuang(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增时默认状态为未审核（1）
            this.form.status = 1;
            console.log(this.form);
            AddDatazongchuang(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除所选的报表？")
        .then(() => {
          return Deatalzongchuangdetial(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>

<style scoped>
.header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.el-card {
  margin-bottom: 20px;
}
</style>
